package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.Beer;
import Models.Documents;

public class BeerPresenter extends AbstractPresenter<Beer> {
	final String SINGLEITEM = "beer";
	final String MULTIITEM = "beers";
	
	public ArrayList<Beer> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Beer tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public Beer createItemFromJSONObject(JSONObject jsonObject){
		Beer item = new Beer();
		try{
			item.setBeerABV(jsonObject.getDouble("BAbv"));
			item.setBeerConditioningDays(jsonObject.getInt("beerCondDays"));
			item.setBeerCOVols(jsonObject.getDouble("BCOVols"));
			item.setBeerFermentationDays(jsonObject.getInt("beerFermDays"));
			item.setBeerFilter(jsonObject.getBoolean("beerFilter"));
			item.setBeerFine(jsonObject.getBoolean("beerFine"));
			item.setBeerIBU(jsonObject.getDouble("BIbu"));
			item.setBeerID(jsonObject.getInt("BId"));
			item.setBeerName(jsonObject.getString("BName"));
			item.setBeerNotes(jsonObject.getString("BNotes"));
			item.setBeerSecondaryDays(jsonObject.getInt("beerSecondDays"));
			item.setBeerSRM(jsonObject.getDouble("BSrm"));
			item.setInProduction(jsonObject.getBoolean("inProduction"));
			
			if(!jsonObject.isNull("BCost")){
				//create product if set
				CostPresenter presenter = new CostPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("BCost");
				item.setBeerCost(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBeerCost(null);
			}
			
			if(!jsonObject.isNull("recipeID")){
				//create product if set
				RecipePresenter presenter = new RecipePresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("recipeID");
				item.setBeerRecipeID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBeerRecipeID(null);
			}
			
			//Set Documents Collection
			ArrayList<Documents> documents = new ArrayList<Documents>();
			if(!jsonObject.isNull("documentsCollection")){
				Object itm = jsonObject.get("documentsCollection");
				DocumentPresenter presenter = new DocumentPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						documents.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					documents.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setDocs(documents);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Beer item){
		try{
			JSONObject json = new JSONObject();
			json.put("BAbv", item.getBeerABV());
			json.put("beerCondDays", item.getBeerConditioningDays());
			json.put("BCOVols", item.getBeerCOVols());
			json.put("beerFermDays", item.getBeerFermentationDays());
			json.put("beerFilter", item.isBeerFilter());
			json.put("beerFine", item.isBeerFine());
			json.put("BIbu", item.getBeerIBU());
			json.put("BId", item.getBeerID());
			json.put("BName", item.getBeerName());
			json.put("BNotes", item.getBeerNotes());
			json.put("beerSecondDays", item.getBeerSecondaryDays());
			json.put("BSrm", item.getBeerSRM());
			json.put("inProduction", item.getInProduction());

			if(item.getBeerCost()!=null){
				final CostPresenter presenter = new CostPresenter();
				json.put("BCost", presenter.createJSON(item.getBeerCost()));
			} else {
				json.put("BCost", JSONObject.NULL);
			}

			if(item.getBeerRecipeID()!=null){
				final RecipePresenter presenter = new RecipePresenter();
				json.put("recipeID", presenter.createJSON(item.getBeerRecipeID()));
			} else {
				json.put("recipeID", JSONObject.NULL);
			}
			
			//Create Doc Collection
			if(item.getDocs()!=null){
				DocumentPresenter presenter = new DocumentPresenter();
				if(item.getDocs().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getDocs().size(); i++){
						jsonArray.put(presenter.createJSON(item.getDocs().get(i)));
					}
					json.put("documentsCollection", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Beer getEmptyEntity(){
		Beer entity = new Beer();
		return entity;
	}
}
